/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : motor.hpp
  * Description        : 
  ******************************************************************************
  * Function           :
  *
  ******************************************************************************
  * Author             : Zhiping Fu
  * Creation Date      : Oct 23, 2020
  ******************************************************************************
  */
/* USER CODE END Header */

#ifndef __MOTOR_HPP
#define __MOTOR_HPP

#include "userlib/userlib.hpp"
#include "system/system.hpp"

#ifdef __cplusplus
extern "C" {
#endif

#define quadrocopter
//#define helicopter
//#define duct

#define PWM_OUT_NUM 4
#define ESC_CMD_BUFFER_LEN	18
#define ESC_ERPM_BUFFER_LEN 20
#define MOTOR_BIT_1  20
#define MOTOR_BIT_0  10
#define INI_PWM 0
#define Min_PWM_Out  0 //0
#define Max_PWM_Out  2047//2047

#define NUM_OF_MOTOR_POLE_PAIR	6

#define iv 0xffffffff

static const uint32_t decode[32] = {
	iv, iv, iv, iv, iv, iv, iv, iv, iv, 9, 10, 11, iv, 13, 14, 15,
	iv, iv, 2, 3, iv, 5, 6, 7, iv, 0, 8, 1, iv, 4, 12, iv };

void DMA2_Stream3_IRQHandler(void);
void DMA2_Stream2_IRQHandler(void);
void DMA2_Stream6_IRQHandler(void);
void DMA2_Stream4_IRQHandler(void);

#ifdef __cplusplus
}
#endif


#ifdef __cplusplus

using namespace std;

class MOTOR
{
public:
	MOTOR(){}
	MOTOR(TIM_TypeDef *ha,char *n) : htimA(ha),name(n){}
	MOTOR(TIM_TypeDef *ha,TIM_TypeDef *hb,char *n) : htimA(ha),htimB(hb),name(n){}
	~MOTOR(){}

	void motor_Init(void);

	void motor_Start(void);

	void motor_run(void);

	uint16_t erpm_Pwm_In[PWM_OUT_NUM][ESC_ERPM_BUFFER_LEN];
	uint8_t DMA_TC_FLAG_NUM;				//用来记录每个控制周期发送完成的油门信号帧数，当为PWM_OUT_NUM时表示所有电机油门信号帧发送完成，可以将自动重装载值改为65535以待进行输入捕获


private:
	LL_TIM_OC_InitTypeDef TIM_OC_InitStruct;
	TIM_TypeDef *htimA;
	TIM_TypeDef *htimB;
	char *name;

	eSTA  Sta;        //状态  --
	eERR  Err;        //错误信息  --
	bool  UnLock;     //解锁
	uint8_t requestTelemetry;

	uint32_t  startTimer;			//计时器
	uint32_t  stopTimer;			//计时器
	uint32_t  executionTime_us;			//计时器

	uint32_t  startTimerLast;			//计时器
	uint32_t  cycleTime_us;

	uint16_t ESC_CMD[PWM_OUT_NUM][ESC_CMD_BUFFER_LEN];	//DSHOT300

	uint16_t PWM[PWM_OUT_NUM];

	uint32_t erpm_Code_Edge[PWM_OUT_NUM];
	uint32_t erpm_Code_GCR[PWM_OUT_NUM];
	uint32_t erpm_Code[PWM_OUT_NUM];

	uint32_t erpm_us[PWM_OUT_NUM];
	uint32_t erpm[PWM_OUT_NUM];
	float rpm[PWM_OUT_NUM];

	motor_rpm_msg motorRpm;
	RC_command_msg rcCommand;
	Motor_PWM_msg motorPWM;

};

#endif


#endif


/************************ (C) COPYRIGHT Longmen Drone Team *****END OF FILE****/
